﻿using IService;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IEFContext;
using Microsoft.EntityFrameworkCore;

namespace Service {
  public class MenuService : ServiceBase, IMenuService {
    public MenuService(IEFContext.IEFContext eFContext) : base(eFContext) {

    }
    public List<MenuInfo> GetMenus() {
      return (from menu in Context.Set<MenuInfo>() where menu.State == 1 select menu).ToList();
    }

    public List<MenuInfo> GetMenusByRoleId(int roleId) {
      var roles = (from role in Context.Set<RoleInfo>()
                   where role.RoleId == roleId && role.state == 1
                   select role.RoleId).ToList();
      var query = from menu in Context.Set<MenuInfo>()
                  join role_menu in Context.Set<RoleMenu>()
                  on menu.MenuId equals role_menu.MenuId
                  where roles.Contains(role_menu.RoleId) && menu.State == 1
                  select menu;
      return query.Distinct().ToList();

    }

    public List<MenuInfo> GetMenusByUserId(int userId) {
      var roles = (from user_role in Context.Set<UserRole>()
                   join role in Context.Set<RoleInfo>()
                   on user_role.RoleId equals role.RoleId
                   where user_role.UserId == userId && role.state == 1
                   select user_role.RoleId).ToList();
      var query = from menu in Context.Set<MenuInfo>()
                  join role_menu in Context.Set<RoleMenu>()
                  on menu.MenuId equals role_menu.MenuId
                  where roles.Contains(role_menu.RoleId) && menu.State == 1
                  select menu;
      return query.Distinct().ToList();
    }

    public void SaveMenu(string data) {
      var value = Newtonsoft.Json.JsonConvert.DeserializeObject<MenuInfo>(data);
      if (value.MenuId == 0) {
        var index = Context.Set<MenuInfo>().Max(i => i.Index);
        value.Index = index + 1;
      }
      value.State = 1;
      Context.Entry(value).State = value.MenuId == 0 ? EntityState.Added : EntityState.Modified;
      Context.SaveChanges();
    }
  }
}
